//
//  Key
//
//  Created by Caue W. on 2011-03-04.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
package time.types.ds;
import time.runtime.ObjectStore;

@ValueType class Key<T>
{
	public var kind(get_kind, never):String;
	public var id(get_id, never):String;
	public var store(get_store, never):ObjectStore;
	
	private var __kind:String;
	private var __id:String;
	
	@Ignore private var __store:ObjectStore;
	@Ignore private var __object:T;
	@Ignore private var __toString:String;
	
	public function new(kind:String, id:String, store:ObjectStore) : Void
	{
		this.__kind = kind;
		this.__id = id;
		this.__store = store;
	}
	
	inline function get_store()
	{
		return __store;
	}
	
	public inline function get():T
	{
		return if (__object != null)
			__object;
		else
			__object = __store.get(this);
	}
	
	public function equals(k:Key<T>) : Bool
	{
		return __id == k.__id && __kind == k.__kind;
	}
	
	public inline function clone() : Key<T>
	{
		return new Key<T>(__kind, __id, __store);
	}
	
	public inline function toString() : String
	{
		return (__toString != null) ? __toString : (__toString = __kind + "::" + __id);
	}
	
	private inline function get_kind() : String
	{
		return __kind;
	}
	
	private inline function get_id() : String
	{
		return __id;
	}
}